import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:fantuike_app/api/Api.dart' show Api;
import 'package:fantuike_app/constants.dart' show AppColors;
import 'child_page/product_detail.dart';

class LoanListPage extends StatefulWidget {
  @override
  _LoanListPageState createState() => _LoanListPageState();
}

class _LoanListPageState extends State<LoanListPage> {
  List creditcardList = [];
  int total = 0;

  PreferredSize BuildAppBarWidget() {
    return PreferredSize(
        child: AppBar(
          leading: IconButton(
              icon: Icon(
                Icons.chevron_left,
                size: 30.0,
                color: Color(0xffffffff),
              ),
              onPressed: () {
                Navigator.pop(context);
              }),
          backgroundColor: Color(0xfff26966),
          title: Text(
            "贷款",
            style: TextStyle(
                color: Color(
                  0xffffffff,
                ),
                fontSize: 18.0),
          ),
          centerTitle: true,
          elevation: 0.0,
        ),
        preferredSize:
        Size.fromHeight(MediaQuery.of(context).size.height * 0.065));
  }

  @override
  void initState() {
    super.initState();
    getCreditCardList();
  }

  void getCreditCardList() {
    _getCreditCardList().then((res) {
      print(res['extend']['message']['list']);
      setState(() {
        creditcardList.addAll(res['extend']['message']['list']);
        total = res['extend']['message']['total'];
      });
    });
  }

  Future _getCreditCardList() async {
    try {
      Response response = await Dio().post(Api.getCreditCardList,
          data: {"ids": [], "loanType": 0, "productType": 1, "pn": 1});
      return response.data;
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> tiles = [];

    for (var item in creditcardList) {
      tiles.add(GestureDetector(
          onTap: () {
            Navigator.of(context).push(new MaterialPageRoute(builder: (context) => ProductDetailPage(productId: item['id'], productType: 1)));
          },
          child: new Container(
            margin: const EdgeInsets.only(left: 10.0, right: 10.0),
            width: 175.0,
            child: Column(
              children: <Widget>[
                ClipRRect(
                  borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(8.0),
                      topRight: Radius.circular(8.0)),
                  child: Image.network(
                    item['firstPicture'],
                    fit: BoxFit.fill,
                  ),
                ),
                Container(
                  alignment: Alignment.center,
                  margin:
                  const EdgeInsets.only(left: 3.0, right: 3.0, top: 2.0),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: <Widget>[
                      Text(item['name'],
                          style: TextStyle(
                              color: Color(0xff323232), fontSize: 15.0)),
                      Container(
                        margin: const EdgeInsets.only(top: 2.0),
                        padding: const EdgeInsets.only(
                            left: 1.0, right: 1.0, top: 1.0, bottom: 1.0),
                        alignment: Alignment.center,
                        child: Text(
                          item['settlementType'],
                          style: TextStyle(
                              color: Color(0xffFFAE51), fontSize: 10.0),
                        ),
                        decoration: BoxDecoration(
                            border: Border.all(
                                width: 1.0, color: Color(0xffFFAE51)),
                            borderRadius:
                            BorderRadius.all(Radius.circular(3.0))),
                      )
                    ],
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(left: 3.0, top: 3.0),
                  alignment: Alignment.centerLeft,
                  child: Text(
                    "通过率: " + item['passRate'],
                    style: TextStyle(color: Color(0xff969696), fontSize: 11.0),
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(left: 3.0),
                  alignment: Alignment.centerLeft,
                  child: Text(
                    "已发放奖金: " + item['haveBonus'] + "元",
                    style: TextStyle(color: Color(0xff969696), fontSize: 11.0),
                  ),
                ),
                Container(
                  margin: const EdgeInsets.only(top: 5.0, bottom: 4.0),
                  padding: const EdgeInsets.only(top: 2.0, bottom: 2.0),
                  width: 145.0,
                  alignment: Alignment.center,
                  child: Text(
                    item['moneyDescription'],
                    style: TextStyle(color: Color(0xffffffff), fontSize: 12.0),
                  ),
                  decoration: BoxDecoration(
                      color: Color(0xffF26966),
                      borderRadius: BorderRadius.all(Radius.circular(10.0))),
                )
              ],
            ),
            decoration: BoxDecoration(
                border: Border.all(width: 1.0, color: Color(0xffF5F5F5)),
                // color: Colors.lightBlue,
                borderRadius: BorderRadius.all(Radius.circular(9.0))),
          )));
    }

    return Scaffold(
      appBar: BuildAppBarWidget(),
      body: SingleChildScrollView(
        child: Container(
            color: Color(AppColors.AppBackColor),
            child: Column(
              children: <Widget>[
                Container(
                  margin: const EdgeInsets.only(top: 15.0, left: 10.0),
                  alignment: Alignment.centerLeft,
                  child: Row(
                    children: <Widget>[
                      Container(
                        margin: const EdgeInsets.only(right: 3.0),
                        width: 5.0,
                        height: 20.0,
                        color: Color(0xfff26966),
                      ),
                      Text(
                        "当前共有"+total.toString()+"个产品",
                        style: TextStyle(
                            color: Color(0xfff26966),
                            fontSize: 18.0,
                            fontWeight: FontWeight.bold),
                      )
                    ],
                  ),
                ),
                ListView(
                  physics: NeverScrollableScrollPhysics(),
                  scrollDirection: Axis.vertical,
                  shrinkWrap: true,
                  padding: const EdgeInsets.all(0.0),
                  children: <Widget>[
                    GridView.count(
                        childAspectRatio: 1 / 1,
                        mainAxisSpacing: 10.0,
                        crossAxisSpacing: 0.0,
                        padding: const EdgeInsets.only(
                            top: 10.0, bottom: 5.0, left: 5.0, right: 5.0),
                        physics: NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        crossAxisCount: 2,
                        children: tiles)
                  ],
                )
              ],
            )),
      ),
    );
  }
}
